<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\PosterInstitutionFormat;
use App\Http\Requests\CreatePosterInstitutionRequest;
use App\Services\PosterInstitutionService;
use Illuminate\Http\Request;

class PosterInstitutionController extends Controller
{
    protected $posterInstitutionService;

    public function __construct(PosterInstitutionService $posterInstitutionService)
    {
        $this->posterInstitutionService = $posterInstitutionService;
    }

    public function create(CreatePosterInstitutionRequest $createPosterInstitutionRequest)
    {
        $format = new PosterInstitutionFormat($createPosterInstitutionRequest->all());
        $this->posterInstitutionService->create($format);
        return renderSuccessJson();
    }

    public function getLists(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }

        $res = $this->posterInstitutionService->getList($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }

        $res = $this->posterInstitutionService->getAdminList($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function update(Request $request)
    {
        $format = new PosterInstitutionFormat($request->all());
        $this->posterInstitutionService->update($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->posterInstitutionService->delete($id);
        return renderSuccessJson();
    }
}
